<%@page import="com.feib.soeasy.model.UserLoginControl"%>
<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.action.Soez9001ActionBean"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<%@page import="com.feib.soeasy.util.CodeTableUtil"%>
<%@page import="com.feib.soeasy.model.User"%>
<c:choose>
	<c:when test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
		<c:set var="lastPageNumber"
			value="${actionBean.queryResult.totalPageCount}" />
		<c:set var="currentPageNumber"
			value="${actionBean.queryResult.pageNumber}" />
		<c:set var="prePageNumber"
			value="${actionBean.queryResult.pageNumber - 1}" />
		<c:set var="nextPageNumber"
			value="${actionBean.queryResult.pageNumber + 1}" />
		
			<div id="paginationBar" colspan="10"><input type="button" id="firstPageBtn" value="第一頁"
				<c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
				<input type="button" id="prePageBtn" value="上一頁"
					<c:out value='${prePageNumber<=0 ? "disabled": "" }'/> /> 第 
						<input type="text" id="currentPageText" value="${currentPageNumber }"
						size="3" maxlength="4"> / ${lastPageNumber} 頁 <input
						type="button" id="nextPageBtn" value="下一頁"
						<c:out value='${lastPageNumber<nextPageNumber ? "disabled": "" }'/> />
					<input type="button" id="lastPageBtn" value="最末頁"
						<c:out value='${lastPageNumber<=currentPageNumber ? "disabled": "" }'/> />
			</div>
			<%
				// 當查詢到使用者不等於目前登入使用者群組代號，則要顯示業者代號欄位
				Soez9001ActionBean action = (Soez9001ActionBean)request.getAttribute("actionBean");
				User user = action.getQueryResult().getPaginatedList().get(0);
				if (null != user && !UserUtil.getUserGroup().getGroupNo().equals(user.getGroup().getGroupNo()))
				    pageContext.setAttribute("showGroupName", true);
				else
				    pageContext.setAttribute("showGroupName", false);				    
			%>
			<table id="listTable">
			<thead>
				<tr>
					<c:if test="${showGroupName}"><th>業者名稱</th></c:if>					
					<th>使用者ID</th>
					<th>姓名</th>
					<th>電話</th>
					<th>E-MAIL</th>
					<th>登入狀態</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				<c:forEach items="${actionBean.queryResult.paginatedList}" var="viewObj" varStatus="rowstat">
					<tr>
						<c:if test="${showGroupName}"><td><c:out value="${viewObj.group.groupName}" /></td></c:if>
						
						<td><c:out value="${viewObj.userId}" /></td>
						<td><c:out value="${viewObj.userName}" /></td>
						<td><c:out value="${viewObj.userPhone}" /></td>
						<td><c:out value="${viewObj.userEmail}" /></td>
						<td>
						<%
						user = (User)pageContext.getAttribute("viewObj");
						UserLoginControl c = action.getUserLoginControlByUser(user);
						if (null == c || null == c.getActived() || !c.getActived().booleanValue())
						    out.println("離線");
						else if (c.getActived())
						    out.println("登入");
						
						c = null;
						%>
						</td>
						<td>
							<div id="toAjaxEditImg-<c:out value='${viewObj.userPk}'/>"><a href="javascript: void(0)" class="btnZoom">修改</a></div>
							
						</td>
					</tr>
				</c:forEach>
			</tbody>
		</table>

		<script type="text/javascript">
<!--
	$(document).ready(function() {
		$("[id^='firstPageBtn']").click(function() {
			doQueryAction(1);
		});

		$("[id^='prePageBtn']").click(function() {
			doQueryAction(<c:out value='${prePageNumber}'/>);
		});

		$("[id^='nextPageBtn']").click(function() {
			doQueryAction(<c:out value='${nextPageNumber}'/>);
		});

		$("[id^='lastPageBtn']").click(function() {
			doQueryAction(<c:out value='${lastPageNumber}'/>);
		});

		$("[id^='currentPageText']").change(function() {
			var value = this.value;
			if (value != '') {
				value = value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
				var intRegex = /^\d+$/;
				if (!intRegex.test(value)) {
					alert('頁數必須是整數');
					return;
				}

				value = parseInt(value, 10);

				if (value < 1) {
					alert('頁數必須是大於1');
					return;
				} else if (value > <c:out value='${lastPageNumber}'/>) {
					alert('頁數必須是小於總頁數 (<c:out value='${lastPageNumber}'/>)');
					return;
				} else if (value != <c:out value='${currentPageNumber}'/>) {
					doQueryAction(value);
				}

			} else {
				alert('頁數不可為空白');
				return;
			}
		});

		$("[id^='toAjaxEditImg-']").click(function() {
			var userPk = this.id.substring(14);

			waitingDialog({});
			$.ajax({
				url : "<c:url value='/ajax/Soez9001.action'/>",
				async : true,
				type : 'POST',
				data : {toAjaxEditUser: 'toAjaxEditUser', 'editUser.userPk': userPk},
				dataType : 'html',
				success : function(data, textStatus, XMLHttpRequest) {
					$("#addResultDiv").html(data);
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
					$("#ajaxErrorMessageDiv").dialog("destroy");
					$("#ajaxErrorMessageDiv").dialog({
						title : '系統錯誤',
						width : 600,
						resizable : false,
						modal : true,
						buttons : {
							Ok : function() {
								$(this).dialog("close");
							}
						}
					});
				},
				complete : function(XMLHttpRequest, textStatus) {
					closeWaitingDialog();
				}
			});
		});

	});
//-->
</script>
	</c:when>
	<c:otherwise>
		<div id="queryNothingBlockDiv" style="display: none;">查無資料</div>
		<script type="text/javascript">
<!--
	$(document).ready(function() {
		$("#queryNothingBlockDiv").dialog("destroy");
		$("#queryNothingBlockDiv").dialog({
			title : '<%=UserUtil.getCurrentMenuItemName() %>查詢結果',
			width : 300,
			resizable : false,
			modal : true,
			buttons : {
				Ok : function() {
					$(this).dialog("close");
				}
			}
		});
	});
//-->
</script>
	</c:otherwise>
</c:choose>